<template>
    <div class="login_in">
        <div class="text">登 录</div>
        <div class="input">
            <el-input size="large" :prefix-icon="UserFilled" v-model="username" placeholder="用户名">
            </el-input>
            <el-input size="large" :prefix-icon="Grid" v-model="password" placeholder="密码" type="password"
                show-password>
            </el-input>
            <el-button @click="Login" size="large" type="primary" plain>登录</el-button>
        </div>


    </div>
</template>

<script setup lang="ts">
import { Grid, UserFilled } from '@element-plus/icons-vue'
import { getLogin } from '@/server/login';
import { ref } from 'vue'
import { useRouter } from 'vue-router';
import { useTabStore } from '@/store/useTabStore';
import { ElNotification } from 'element-plus'
const router = useRouter()
const username = ref('')
const password = ref('')
const tabStore = useTabStore()
const Login = () => {
    //数据为空
    if (!username.value || !password.value) {
        ElNotification({
            title: '登录',
            message: '输入不能为空',
            type: 'error',
        })
        return
    }

    const data = {
        username: username.value,
        password: password.value
    }
    getLogin(data).then(res => {
        console.log(res)
        //存储token
        localStorage.setItem('token', res.data.token)
        //更新用户名
        tabStore.setUserName(res.data.username)
        //修改登录状态
        tabStore.setLoginStatus(true)
        //更新用户头像
        tabStore.setAvatar(res.data.avatar)
        //更新用户id
        tabStore.setUserId(res.data.id)
        //跳转页面
        router.push('/home')
        //清空
        username.value = ''
        password.value = ''
    }).catch(err => {
        console.log(err)
        ElNotification({
            title: '登录',
            message: '用户名或密码错误',
            type: 'error',
        })
    })
}

</script>

<style scoped lang="scss">
.login_in {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    height: 100%;

    .text {
        font-size: 30px;
        font-weight: bold;
        margin-bottom: 20px;
    }

    .input {
        width: 300px;

        .el-input {
            margin: 15px 0;
        }

        .el-button {
            margin: 15px 0;
            font-weight: bold;
            width: 100%;
        }
    }





}
</style>